package com.wang.utils.hash;

/**
 * @author 10570
 * @date 2021/8/8 15:27
 */

import javax.xml.stream.FactoryConfigurationError;
import java.text.BreakIterator;
import java.util.Scanner;

/**
 * hash表管理雇员
 */
public class HashTabDemo {
    public static void main(String[] args) {
        //创建hash
        HashTab hashTab = new HashTab(7);
        String key = "";
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("add：添加");
            System.out.println("list：显示");
            System.out.println("exit：退出");
            System.out.println("find：查找");
            System.out.println("delete：删除");
            key = scanner.next();
            switch (key){
                case "add":
                    System.out.println("输入id");
                    int id = scanner.nextInt();
                    System.out.println("输入名字");
                    String name = scanner.next();
                    Emp emp = new Emp(id,name);
                    hashTab.add(emp);
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                case "find":
                    System.out.println("输入id");
                    int id1 = scanner.nextInt();
                    hashTab.findById(id1);
                    break;
                case "delete":
                    System.out.println("输入id");

                    int id2 = scanner.nextInt();
                    hashTab.delete(id2);
                    break;
                default:
                    break;
            }
        }

    }
}

//创建hashtable
class HashTab{
    private EmpLinkedList[] empLinkedListArray;
    private int size; //用多少条链表
    //构造器

    public HashTab(int size) {
        //初始化链表
        this.size = size;
        empLinkedListArray = new EmpLinkedList[size];
        //不要忘了，分别初始化每一条链表
        for (int i = 0;i < size;i++){
            empLinkedListArray[i] = new EmpLinkedList();
        }

    }
    //添加雇员
    public void add(Emp emp){
        //根据员工的id得到该员工，应该添加到那条链表
        int empLinkedListNO = hashFun(emp.id);
        empLinkedListArray[empLinkedListNO].add(emp);
    }
    //遍历所有的链表
    public void list(){
        //遍历hash表   数组加上hashtab
        for (int i = 0 ; i< size;i++){
            empLinkedListArray[i].list(i);
        }
    }

    //编写一个散列函数，使用取模法
    public int hashFun(int id){
        return id%size;
    }

    //根据输入的id查找雇员
    public void findById(int id){
        //使用散列确定去那条链表
        int empLinkedListNO = hashFun(id);
        Emp emp = empLinkedListArray[empLinkedListNO].findById(id);
        if (emp !=null){
            System.out.printf("第%d 条链表中找到\n",(empLinkedListNO+1));
        }else {
            System.out.println("没有找到");
        }
    }

    //删除id
    public void delete(int id){
        int empLinkedListNO = hashFun(id);
        empLinkedListArray[empLinkedListNO].delete(id);
    }
}

class Emp{
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
}

//表示链表
class EmpLinkedList{
    private Emp head = null; //默认为空


    //添加
    //加在末尾，id自增
    public void add(Emp emp){
        if (head == null){
            head = emp;
            return;
        }
        Emp temp = head;
        while (true){
            if (temp.next == null ){ //链表倒了最后
                break;
            }
            temp = temp.next;

        }
        //退出时，将数据加到最后
        temp.next = emp;
    }

    public void list(int no){
        if (head == null){
            //说明链表为空
            System.out.printf("第%d链表为空 ",no);
            System.out.println();
            return;
        }
        Emp temp = head;
        while (true){
            System.out.printf("第%d链表:id = %d\t name = %s ",no,temp.id,temp.name);
            System.out.println();
            if (temp.next == null){
                return;
            }
            temp = temp.next;//后移
        }
    }

    //根据id查找
    //找到就返回，没找到返回null
    public Emp findById(int id){

        if (head == null){
            System.out.println("为空");
            return null;

        }
        Emp temp = head;
        while (true){
            if (temp.id == id){
                break;  //指向查找的雇员
            }
            //退出条件
            if (temp.next == null){
                temp = null;  //没有找到的话要退出
                break;//退出
            }
            temp = temp.next;
        }
        return temp;
    }

    //删除雇员
    //找到对应id删除，使用的链表删除
    public void delete(int id){
        boolean flag = false;
        if (head == null){
            System.out.println("数据为空");
            return;
        }
        Emp temp = head;
        while (true){
            if (temp.id == id){
                flag = true;
                System.out.println("删除id"+id);
                break;
            }
            if (temp.next ==null){
                temp = null;
                System.out.println("没有找到");
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.next = temp.next.next;
        }
    }

}